package com.dragon.system.controller;


import com.dragon.handler.BusinessException;
import com.dragon.response.Result;
import com.dragon.response.ResultCode;
import com.dragon.system.entity.Department;
import com.dragon.system.service.DepartmentService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author dragon
 * @since 2021-05-26
 */

@RestController
@RequestMapping("/department")
@CrossOrigin
@Api(value = "部门管理",tags = "部门查询")
public class DepartmentController {

    /*@Autowired
    private DepartmentService departmentService;
    @GetMapping("/findDeptAndCount")
    @ApiOperation(value = "查询部门及人数",notes = "从部门表中分组查询用户人数")
    public Result findDeptAndCount(){
        List<Department> departments = departmentService.findDeptAndCount();
        if (departments.size()==0){
            throw new BusinessException(ResultCode.DEPARTMENT_NOT_EXIST.getCode(),
                    ResultCode.DEPARTMENT_NOT_EXIST.getMessage());
        }
        return Result.ok().data("departments",departments);
    }*/
    @Autowired
        private DepartmentService departmentService;
        @ApiOperation(value = "统计查询部门",notes = "统计查询部门")
        @GetMapping("/findDeptAndCount")
        public Result findDeptAndCount(){
            List<Department> deptAndCount = departmentService.findDeptAndCount();
            if (deptAndCount.size()==0){
                throw new BusinessException(ResultCode.DEPARTMENT_NOT_EXIST.getCode(),ResultCode.DEPARTMENT_NOT_EXIST.getMessage());
            }else {
                return Result.ok().data("department",deptAndCount);
            }
    }
}

